package com.site.wisdom_construction.controller.iotModule;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.iotModule.DustMonitoringService;
import com.site.wisdom_construction.util.HttpRequest;
import com.site.wisdom_construction.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2022/7/14
 * @Description
 */
@Api(tags = "扬尘监测接口")
@RestController
@RequestMapping("DustMonitoring")
@CrossOrigin
public class DustMonitoringController {
    @Autowired
    private DustMonitoringService dustMonitoringService;

    @ApiOperation(value = "分页模糊查询扬尘监测数据", notes = "必传参数:pageNo,pageSize,projectId;选传参数:startTime,endTime")
    @PostMapping("/findDustMonitoringByPage")
    public Page findDustMonitoringByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(dustMonitoringService.Total(map));
        page.setItems(dustMonitoringService.findDustMonitoringByPage(map));
        return page;
    }

    @ApiOperation(value = "查询最新一条扬尘监测数据", notes = "必传参数:projectId")
    @PostMapping("/findDustMonitoringByRecently")
    public OneDataResult findDustMonitoringByRecently(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(dustMonitoringService.findDustMonitoringByRecently(map));
        return oneDataResult;
    }

    @ApiOperation(value = "删除扬尘监测数据", notes = "参考传参: list:{1,2,3,4,5,6}")
    @PostMapping("/deleteContractWorkers")
    public JsonResult deleteContractWorkers(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        List<Integer> list = (List<Integer>) map.get("list");
        int l = 0;
        Map mapR = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            mapR.put("id", list.get(i));
            int a = dustMonitoringService.deleteDustMonitoring(mapR);
            l += a;
        }
        if (l >= 1) {
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除人员信息!");
        } else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除人员信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "获取最新扬尘监测数据", notes = "参考传参: ")
    @PostMapping("/findDustMonitoringNew")
    public OneDataResult findDustMonitoringNew(@RequestBody Map map){
        HttpRequest httpRequest =new HttpRequest();
        OneDataResult oneDataResult = new OneDataResult();
        RedisUtils redisUtils = new RedisUtils();
        String projectId = (String) map.get("projectId");
        String token = redisUtils.getKey("token_"+projectId);
        if(token.equals("null")){
            String path = "http://dust.0531yun.cn/api/getTokenByAcc";
            String data = httpRequest.sendGet(path,"loginName=c220708jtjs&password=c220708jtjs");
            token = JSONObject.parseObject(data).getJSONObject("data").getString("token");
            redisUtils.setKey("token_"+projectId,token);
        }
        String path = "http://dust.0531yun.cn/api/getTokenByAcc";
        String data = httpRequest.sendGet(path,"loginName=c220708jtjs&password=c220708jtjs");

        return oneDataResult;
    }
}
